package entity

import (
	"fmt"
	"time"

	"shqsoft.cn/pasnj/base"
	"shqsoft.cn/pasnj/workflow"
	"shqsoft.cn/pasnj/workflow/entity"

	"github.com/jinzhu/gorm"
	//log "github.com/sirupsen/logrus"
)

//零售流程运行
type INDILCYX struct {
	ID          int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	LCMC        string    `gorm:"column:流程类型"`
	RDBGRQ      time.Time `gorm:"column:认定变更日期"`
	SQRQ        time.Time `gorm:"column:申请日期"`
	SQR         string    `gorm:"column:申请人"`
	CUNM        string    `gorm:"column:客户名称"`
	IDNO        string    `gorm:"column:证件号码"`
	KHWD        string    `gorm:"column:开户网点"`
	OPDT        time.Time `gorm:"column:开户日期"`
	EMID1       int       `gorm:"column:EMID1"`
	EMNM1       string    `gorm:"column:客户经理1"`
	OrganName1  string    `gorm:"column:所属部门1"`
	DSVL1       float64   `gorm:"column:占比1"`
	EMID2       int       `gorm:"column:EMID2"`
	EMNM2       string    `gorm:"column:客户经理2"`
	OrganName2  string    `gorm:"column:所属部门2"`
	DSVL2       float64   `gorm:"column:占比2"`
	EMID3       int       `gorm:"column:EMID3"`
	EMNM3       string    `gorm:"column:客户经理3"`
	OrganName3  string    `gorm:"column:所属部门3"`
	DSVL3       float64   `gorm:"column:占比3"`
	EMID4       int       `gorm:"column:EMID4"`
	EMNM4       string    `gorm:"column:客户经理4"`
	OrganName4  string    `gorm:"column:所属部门4"`
	DSVL4       float64   `gorm:"column:占比4"`
	EMID5       int       `gorm:"column:EMID5"`
	EMNM5       string    `gorm:"column:客户经理5"`
	OrganName5  string    `gorm:"column:所属部门5"`
	DSVL5       float64   `gorm:"column:占比5"`
	DQZT        string    `gorm:"column:当前状态"`
	DQZB        string    `gorm:"column:当前在办"`
	QZTJ        string    `gorm:"column:强制提交"`
	WJRQ        time.Time `gorm:"column:完结日期"`
	THSM        string    `gorm:"column:THSM"`
	FRID        int       `gorm:"column:FRID"`
	RUNID       int       `gorm:"column:RUNID"`
	WFID        int       `gorm:"column:WFID"`
	Progress    float64   `gorm:"column:process" json:"progress"`
	StepName    string    `gorm:"-" json:"stepName"`
	Status      string    `gorm:"-" json:"status"`
	STEP        int       `gorm:"-" json:"step"`
	GXFH        string    `gorm:"column:GXFH"`
	GXFHCaption string    `gorm:"-"`
}

//TableName Role 零售流程运行
func (INDILCYX) TableName() string {
	return "T_INDI_FLOW_RUN_LOG"
}

// AfterFind 查询后触发器
func (i *INDILCYX) AfterFind(tx *gorm.DB) (err error) {
	if i.GXFH == "0" {
		i.GXFHCaption = "南京分行"
	} else if i.GXFH == "80" {
		i.GXFHCaption = "南通分行"
	} else if i.GXFH == "61" {
		i.GXFHCaption = "无锡分行"
	}

	if i.FRID == 0 {
		i.Status = "exception"
		i.StepName = "校验错误"
		return
	}
	run := entity.FlowRun{}
	if err = tx.Find(&run, i.FRID).Error; err != nil {
		tx.Rollback()
		return
	}
	wf := workflow.GetWorkFlow(run.WFID)
	if wf == nil {
		return fmt.Errorf("指定的流程ID:%d在流程配置中不存在！", run.WFID)
	}
	act := wf.GetAction(run.Step)
	if act == nil {
		return fmt.Errorf("指定的stepID:%d在流程%s配置中不存在！", run.Step, wf.Name)
	}
	count := len(wf.Actions)
	i.STEP = run.Step
	i.StepName = "审批中"
	if i.STEP == 1 {
		i.StepName = "申请中"
	}
	i.Progress = float64(act.Step*100) / float64(count)
	if i.Progress >= 100 && run.State == "0" {
		i.Progress = 95
	}
	i.Status = ""
	if run.State == "0" {
		i.Status = "active"
	} else if run.State == "2" {
		i.StepName = "已中止"
		i.Status = "exception"
	} else {
		i.StepName = "已完成"
		i.Status = "success"
	}
	return
}

func INDILCYXEF() base.EntityFunc {
	return func() interface{} {
		entity := INDILCYX{}
		return &entity
	}
}
func INDILCYXESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []INDILCYX
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("INDILCYX", base.NewDefaultDao(INDILCYXEF(), INDILCYXESF(), nil))
}
